var RevisitRecord = {//回访记录初始化
    id: "RevisitRecordTable",
    seItem: null,
    table: null,
    layerIndex: -1
};

function _formatter(value, row, index) {
    if (row.status === "到店") {
        return '<span style="color: green" title="' + value + '">' + value + '</span>'
    } else {
        return '<span title="' + value + '">' + value + '</span>'
    }
}

function _formatterTime(value, row, index) {
    if (row.status === "到店") {
        return "<span style='color: green'>" + value.substring(0, 19) + "</span>"
    } else {
        return value.substring(0, 19);
    }
}

RevisitRecord.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: '', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: '', field: 'customerSexHead ', visible: false, align: 'center', valign: 'middle'},
        {title: '客户姓名', field: 'customerName', align: 'center', valign: 'middle', sortable: true, width: 120,
            formatter: function (value, row, index) {
                if (row.status === "到店") {
                    return "<a href='javascript:;' style='color: green;display: flex;align-items: center;' onclick = 'RevisitRecord.reservationsArrives(" + '"' + row.id + '"' + ")' ><img src='" + row.customerSexHead + "' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                } else {
                    return "<a href='javascript:;' style='display: flex;align-items: center;' onclick = 'RevisitRecord.reservationsNotDetail(" + '"' + row.id + '"' + ")' ><img src='" + row.customerSexHead + "' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                }
            },
        },
        {title: '手机号码', field: 'customerPhone', align: 'center', valign: 'middle', sortable: true, width: 140,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '预约门诊', field: 'outpatientName', align: 'center', valign: 'middle', sortable: true, width: 110,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '到店状态', field: 'status', align: 'center', valign: 'middle', sortable: true, width: 110,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '回访状态', field: 'statuss', align: 'center', valign: 'middle', sortable: true, width: 110,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '一级渠道', field: 'oneChannel', align: 'center', valign: 'middle', sortable: true, width: 110,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '二级渠道', field: 'twoChannel', align: 'center', valign: 'middle', sortable: true, width: 110,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '三级渠道', field: 'thrChannel', align: 'center', valign: 'middle', sortable: true, width: 130,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '性别', field: 'customerSex', align: 'center', valign: 'middle', sortable: true, width: 80,
            formatter: function (value, row, index) {
                var sexValue = value === 1 ? '男' : value === 2 ? '女' : '未知';
                if (row.status === "到店") {
                    return "<span style='color: green'>" + sexValue + "</span>"
                } else {
                    return sexValue;
                }
            }
        },
        {title: '年龄', field: 'customerAge', align: 'center', valign: 'middle', sortable: true, width: 80,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '回访内容', field: 'huifang', align: 'center', valign: 'middle', sortable: true, width: 250,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            },
            cellStyle: function (value, row, index) {
                return {css: {"overflow":"hidden","white-space":"nowrap","text-overflow":"ellipsis"}}
            }
        },
        {title: '跟进人', field: 'follower', align: 'center', valign: 'middle', sortable: true, width: 90,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '建档人', field: 'archiving', align: 'center', valign: 'middle', sortable: true, width: 90,
            formatter: function (value, row, index) {
                return _formatter(value, row, index);
            }
        },
        {title: '建档时间', field: 'filingTime', align: 'center', valign: 'middle', sortable: true, width: 170,
            formatter: function (value, row, index) {
                return _formatterTime(value, row, index);
            }
        },
        {title: '预约时间', field: 'appointmentTime', align: 'center', valign: 'middle', sortable: true, width: 170,
            formatter: function (value, row, index) {
                return _formatterTime(value, row, index);
            }
        }
    ];
    return columns;
};

RevisitRecord.search = function () {
    RevisitRecord.table.refresh({query: RevisitRecord.formParams()});
    // RevisitRecord.huifangTongJi();
};

RevisitRecord.formParams = function () {
    var queryData = {};
    queryData['reach'] = $("#marketInputPublic", parent.document).val();
    queryData['startRevisitTime'] = $("#startRevisitTime").val();
    queryData['startTime'] = $("#startTime").val();
    queryData['endTime'] = $("#endTime").val();
    queryData['startFilingTime'] = $("#startFilingTime").val();
    queryData['endFilingTime'] = $("#endFilingTime").val();
    queryData['status'] = $("#status").val();   //到店状态
    queryData['archiving'] = $("#archiving").val();   //建档人
    queryData['follower'] = $("#follower").val();
    queryData['department'] = $("#department").val();
    queryData["outpatientName"] = multipleSelection($('#outpatientName').val());
    queryData["area"] = multipleSelection($('#area').val());
    return queryData;
};

var parseParam = function (param) {
    var paramStr = "";
    $.each(param, function (i) {
        paramStr += '&' + getSingleData(param, i);
    });
    return paramStr.substr(1);
};
var getSingleData = function (param, key) {
    return key + "=" + param[key];
};

// RevisitRecord.huifangTongJi = function () {//统计总人数
//     var param1 = $("#department").val();  //部门
//     var param2 = multipleSelection($('#outpatientName').val());
//     var param3 = $("#startRevisitTime").val();  //开始到店时间
//     var param4 = $("#follower").val();  //跟进人
//     var param5 = $("#startFilingTime").val();  //建档时间
//     var param6 = $("#endFilingTime").val();  //建档时间
//     var param7 = $("#startTime").val();
//     var param8 = $("#endTime").val();
//     var date = new Date();
//     var currentWeek = date.getDay();
//     var stepSunDay = -currentWeek + 1;
//     if (currentWeek == 0) {stepSunDay = -7;}
//     var stepMonday = 7 - currentWeek;
//     var time = date.getTime();
//     var monday = new Date(time + stepSunDay * 24 * 3600 * 1000);
//     var sunday = new Date(time + stepMonday * 24 * 3600 * 1000);
//     var param9 = setDateTime(monday,0) + " " + "00:00:00";
//     var param10 = setDateTime(sunday,0) + " " + "23:59:59";
//     var param11 = multipleSelection($('#area').val());
//     var param12 =  $("#marketInputPublic", parent.document).val();  //姓名   电话   微信  预约号
//     var param13 = $("#status").val();  //到店状态
//     var param14 = $("#archiving").val(); //建档人
//     $.ajax({
//         type: "POST",
//         url: Feng.ctxPath + "/reservationRevisit/totalNum",
//         data: {
//             param1: param1, param2: param2, param3: param3,
//             param4: param4, param5: param5, param6: param6,
//             param7: param7, param8: param8, param9: param9,
//             param10: param10, param11: param11, param12: param12,
//             param13: param13, param14: param14
//         },
//         success: function (data) {
//             $("#huifangToday").html(data[0].countNum);//今日
//             $("#huifangYesterday").html(data[1].countNum);//昨日
//             $("#huifangThisWeek").html(data[2].countNum);//昨日
//             $("#huifangMonth").html(data[3].countNum);//本月
//             $("#huifangLastMonth").html(data[4].countNum);//同比
//         }
//     });
// };

RevisitRecord.getData = function () {//片区筛选
    var area = multipleSelection($('#area').val());
    $.post(Feng.ctxPath + "/dept/getListByArea/"+area,function (data) {
        $('#outpatientName').empty();
        for (var i = 0; i < data.length; i++) {
            $('#outpatientName').append("<option value=" + data[i].simplename + ">" + data[i].simplename + "</option>");
        }
        $('#outpatientName').selectpicker('refresh');
        $('#outpatientName').selectpicker('render');
        RevisitRecord.table.refresh({query: RevisitRecord.formParams()});
        // RevisitRecord.huifangTongJi();;
    }, "json");
};

// 选择建档人
RevisitRecord.getArchiving = function () {
    var index = layer.open({
        type: 2,
        title: '选择建档人',
        area: ['70%', '60%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/reservation/to_archiving'
    });
    this.layerIndex = index;
};

RevisitRecord.getFollower = function () {//添加跟进人
    var index = layer.open({
        type: 2,
        title: '添加跟进人',
        area: ['70%', '60%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/reservations/to_follower'
    });
    this.layerIndex = index;
};

RevisitRecord.reservationsArrives = function (id) {
    var index = layer.open({
        type: 2,
        title: '到店客户详情',
        area: ['60%', '100%'], //宽高
        fix: true, //不固定
        maxmin: true,
        offset: ['0px', '40%'],
        content: Feng.ctxPath + '/reservationsArrive/reservations_arrives/' + id
    });
    this.layerIndex = index;
};

RevisitRecord.reservationsNotDetail = function (id) {//点击表格姓名进入详情 - 未到
    var index = layer.open({
        type: 2,
        title: '预约客户详情',
        area: ['60%', '100%'], //宽高
        fix: true, //不固定
        maxmin: true,
        offset: ['0px', '40%'],
        content: Feng.ctxPath + '/reservationsNotArrive/to_reservationsNotDetail/' + id
    });
    this.layerIndex = index;
};

RevisitRecord.resetSearch = function () {//重置
    $("#startRevisitTime").val(""); //开始时间
    $("#area").selectpicker('deselectAll');
    $("#outpatientName").selectpicker('deselectAll');
    $("#follower").val("");
    $("#status").val("");
    $("#archiving").val("");
    $("#startTime").val(""); //开始时间
    $("#endTime").val(""); //开始时间v
    $("#startFilingTime").val(""); //开始时间
    $("#endFilingTime").val(""); //开始时间v
    $("#department").val("");
    $("#marketInputPublic", parent.document).val("");
    RevisitRecord.search();
};

//导出
RevisitRecord.downLoad = function () {
    var queryData = {};
    queryData['reach'] = $("#marketInputPublic", parent.document).val();
    queryData['startRevisitTime'] = $("#startRevisitTime").val();
    queryData['startTime'] = $("#startTime").val();
    queryData['endTime'] = $("#endTime").val();
    queryData['startFilingTime'] = $("#startFilingTime").val();
    queryData['endFilingTime'] = $("#endFilingTime").val();
    queryData['follower'] = $("#follower").val();
    queryData['status'] = $("#status").val();   //到店状态
    queryData['archiving'] = $("#archiving").val();   //建档人
    queryData["outpatientName"] = multipleSelection($('#outpatientName').val());
    queryData["area"] = multipleSelection($('#area').val());
    var param = parseParam(queryData);
    location.href = Feng.ctxPath + '/reservationRevisit/reservationRevisitExport?' + param;
};

//点击上面状态筛选表格
RevisitRecord.today = function () {
    $("#startTime").val(setDateTime(new Date(),0) + " " + "00:00:00");
    $("#endTime").val(setDateTime(new Date(),0) + " " + "23:59:59");
    RevisitRecord.search();
};

RevisitRecord.yesterday = function () {
    $("#startTime").val(setDateTime(new Date(),1) + " " + "00:00:00");
    $("#endTime").val(setDateTime(new Date(),1) + " " + "23:59:59");
    RevisitRecord.search();
};

RevisitRecord.thisWeek = function () {
    var date = new Date();
    var currentWeek = date.getDay();
    var stepSunDay = -currentWeek + 1;
    if (currentWeek == 0) {stepSunDay = -7;}
    var stepMonday = 7 - currentWeek;
    var time = date.getTime();
    var monday = new Date(time + stepSunDay * 24 * 3600 * 1000);
    var sunday = new Date(time + stepMonday * 24 * 3600 * 1000);
    $("#startTime").val(setDateTime(monday,0) + " " + "00:00:00");
    $("#endTime").val(setDateTime(sunday,0) + " " + "23:59:59");
    RevisitRecord.search();
};

RevisitRecord.thisMonth = function () {
    $("#startTime").val(firstMonthTime(new Date(),0) + " " + "00:00:00");
    $("#endTime").val(setMonthTime(new Date(),0) + " " + "23:59:59");
    RevisitRecord.search();
};

RevisitRecord.lastMonth = function () {
    $("#startTime").val(firstMonthTime(new Date(),1) + " " + "00:00:00");
    $("#endTime").val(setMonthTime(new Date(),1) + " " + "23:59:59");
    RevisitRecord.search();
};

$("#marketSearchAll", parent.document).click(function () {RevisitRecord.search();});//主页搜索

function phonePrivileges(){   //隐藏手机号
    var phonePrivileges=parent.$("#phonePrivileges").val();
    if(phonePrivileges == 2){
        $('#RevisitRecordTable').bootstrapTable('hideColumn', 'customerPhone');
    }
}
var scollPostion = 0;

RevisitRecord.getScrollPosition = function (){//获取表格滚动到什么地方
    scollPostion = $('#RevisitRecordTable').bootstrapTable('getScrollPosition');
};

RevisitRecord.scrollTo = function (){//指定表格滚动到什么地方
    $("#RevisitRecordTable").on('load-success.bs.table',function(data){
        $('#RevisitRecordTable').bootstrapTable('scrollTo', scollPostion);
    });
};

$(function () {
    var defaultColunms = RevisitRecord.initColumn();
    var table = new BSTable(RevisitRecord.id, "/reservationRevisit/list1", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(RevisitRecord.formParams());
    RevisitRecord.table = table.init();
    RevisitRecord.today();
    menuNameHideColumn($("#menuName").val(),'RevisitRecordTable');
    $(document).on('click','#laydate_clear,#laydate_today,#laydate_ok',function () {RevisitRecord.search()});
    checkDate = function () {RevisitRecord.search()};
    callbackTable = function () {RevisitRecord.search()};
    $(".btn2").click(function () {
        var stime = document.getElementById("startRevisitTime").value;
        var mtime = stime.substring(0, 7);
        $("#startRevisitTime").val(mtime);
        RevisitRecord.search()
    });
    $(".btn3").click(function () {
        var stime = document.getElementById("startRevisitTime").value;
        $("#startRevisitTime").val(stime);
        RevisitRecord.search()
    });
    $("#accordion").accordion({
        collapsible: true
    });

    $(".followRightHeader > button").click(function () {
        var index = $(this).index();
        $(this).addClass('Selection').siblings().removeClass('Selection');
        $(".followRightContents > div").eq(index).show().siblings().hide();
    });
    phonePrivileges();
});